package com.itheima.app.manager.pmsManager;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itheima.app.exception.BusinessException;
import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.commons.Code;
import com.itheima.commons.PageBean;
import com.itheima.commons.R;
import com.itheima.domain.db.pms.PmsProductCategory;
import com.itheima.service.pmsService.ProductCategoryService;
import com.itheima.vo.pms.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ProductCategoryManager {

    @DubboReference
    ProductCategoryService productCategoryService;

    @Autowired
    OssTemplate ossTemplate;
    //查询所有一级分类及子分类
    public R withChildren() {

        List<PmsProductCategory> categoryList = productCategoryService.findAll();

        if (ObjectUtils.isEmpty(categoryList)) throw new BusinessException("查询数据为空");

        List<PmsProductCategoryVo> resList = new ArrayList<>();

        for (PmsProductCategory category : categoryList) {
            PmsProductCategoryVo pmsProductCategoryVo = new PmsProductCategoryVo();
            BeanUtils.copyProperties(category, pmsProductCategoryVo);
            pmsProductCategoryVo.setChildren(new ArrayList<>());

            // 组装父级
            if (0 == category.getParentId()) {
                for (PmsProductCategory pmsProductCategory : categoryList) {
                    if (category.getId().equals(pmsProductCategory.getParentId())) {
                        pmsProductCategoryVo.getChildren().add(pmsProductCategory);
                    }
                }
                resList.add(pmsProductCategoryVo);
            }
        }

        return new R(Code.SUCCESS, "查询成功", resList);
    }

    public R addProductCategory(PmsProductCategory pmsProductCategory) {

        productCategoryService.addProductCategory(pmsProductCategory);

        return R.ok(null);
    }

    public R deleteProductCategory(Integer id) {

        productCategoryService.deleteProductCategory(id);

        return R.ok(null);
    }

    public R findPageProductCategory(Integer parentId, Integer pageNum, Integer pageSize) {

        PageBean<PmsProductCategory> pageBean = productCategoryService.findPageProductCategory(parentId,pageNum,pageSize);
        return R.ok(pageBean);
    }

    public R updateNavStatus(Integer navStatus, Integer[] ids) {

        productCategoryService.updateNavStatus(navStatus,ids);

        return R.ok(null);
    }

    public R updateShowStatus(Integer[] ids, Integer showStatus) {

        productCategoryService.updateShowStatus(ids,showStatus);

        return R.ok(null);
    }

    public R updateProductCategory(Integer id, PmsProductCategory pmsProductCategory) {

        pmsProductCategory.setId(id.longValue());
        productCategoryService.updateProductCategory(pmsProductCategory);
        return R.ok(null);
    }

    public R selectById(Integer id) {


        return R.ok(productCategoryService.selectById(id));
    }


    public R uploadFile(MultipartFile photoFile) throws IOException {

        String url = ossTemplate.upload(photoFile.getOriginalFilename(), photoFile.getInputStream());

        return R.ok(url);
    }
}
